/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tssp_internal.h"
#include "tssp_table.h"
#include "tssp_pat.h"


struct tssp_pat_monitor {
    struct tssp_table_monitor *tmon;
    tssp_pat_monitor_callback callback;
    void *userdata;
};


struct tssp_pat *tssp_pat_parse(const struct tssp_section *section)
{
    struct tssp_pat *pat;
    struct tssp_pat_program *pat_program;
    uint8_t *pos;
    int ret;
    struct tssp_table_payload p;

    TSSP_ASSERT(section);

    if (section->table_id != TSSP_TABLE_ID_PAT || !section->syntax_indicator)
        return NULL;

    /* prepare data buffer */
    ret = tssp_table_get_payload(&p, section, 0);
    if (ret != TSSP_STATUS_OK)
        return NULL;
    pos = p.start;

    /* allocate pat */
    pat = malloc(sizeof(*pat));
    if (pat == NULL) {
        tssp_table_free_payload(&p);
        return NULL;
    }

    STAILQ_INIT(&pat->programs);
    pat->ts_id = section->extension;
    pat->version = section->version;
    pat->current_next = section->current_next;

    while ((p.end - pos) >= 4) {
        pat_program = malloc(sizeof(*pat_program));
        if (pat_program == NULL) {
            tssp_pat_destroy(pat);
            tssp_table_free_payload(&p);
            return NULL;
        }
        pat_program->number = (pos[0] << 8) + pos[1];
        pat_program->pid = ((0x1f & pos[2]) << 8) + pos[3];
        STAILQ_INSERT_TAIL(&pat->programs, pat_program, entry);
        pos += 4;
    }

    tssp_table_free_payload(&p);
    return pat;
}

void tssp_pat_destroy(struct tssp_pat *pat)
{
    struct tssp_pat_program *tmp;

    if (pat) {
        while ((tmp = STAILQ_FIRST(&pat->programs))) {
            STAILQ_REMOVE_HEAD(&pat->programs, entry);
            free(tmp);
        }
        free(pat);
    }
}


static
void tssp_pat_monitor_section_callback(struct tssp_table_monitor *tmon,
                                       struct tssp_section *section,
                                       void *userdata)
{
    struct tssp_pat_monitor *mon = userdata;
    struct tssp_pat *pat;

    pat = tssp_pat_parse(section);
    if (pat) {
        tssp_table_monitor_set_mask(tmon, 0xFFFF, pat->ts_id);
        mon->callback(mon, pat, section, mon->userdata);
    } else {
        tssp_section_destroy(section);
    }
}

struct tssp_pat_monitor *
tssp_pat_monitor_attach(struct tssp *parser,
                        uint8_t current_next,
                        tssp_pat_monitor_callback callback,
                        void *userdata)
{
    struct tssp_pat_monitor *monitor;
    struct tssp_section_filter filter;

    if (parser == NULL || callback == NULL)
        return NULL;

    monitor = malloc(sizeof(*monitor));
    if (monitor == NULL)
        return NULL;

    tssp_section_filter_init(&filter, TSSP_PID_PAT, TSSP_TABLE_ID_PAT,
                             0, 0, 1, 0);
    monitor->tmon = tssp_table_monitor_attach(parser,
                                              &filter,
                                              tssp_pat_monitor_section_callback,
                                              monitor);
    if (monitor->tmon == NULL) {
        free(monitor);
        return NULL;
    }

    monitor->callback = callback;
    monitor->userdata = userdata;

    return monitor;
}

void tssp_pat_monitor_detach(struct tssp_pat_monitor *monitor)
{
    if (monitor == NULL)
        return;

    TSSP_ASSERT(monitor->tmon);

    tssp_pat_monitor_reset(monitor);
    tssp_table_monitor_detach(monitor->tmon);
    free(monitor);
}

void tssp_pat_monitor_reset(struct tssp_pat_monitor *mon)
{
    TSSP_ASSERT(mon);
    tssp_table_monitor_reset(mon->tmon);
}

